package com.example.administrator.x2picturetransliteration.Base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;


import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.View.SwipeLayout.SwipeLayoutManager;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Method;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.subscriptions.CompositeSubscription;


/**
 * 项目Activity的基类
 * 2018/10/9.
 */


public abstract class BaseActivity extends AppCompatActivity {

    public CompositeSubscription mCompositeSubscriptions; //管理rxjava的订阅关系

    private Unbinder unbinder; //黄油刀

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); //防止录屏

        if (!this.isTaskRoot()) {
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //竖屏
        setContentView(intiLayout()); //绑定布局文件

        mCompositeSubscriptions = new CompositeSubscription(); //实例化CompositeSubscription
        unbinder = ButterKnife.bind(this); //注册黄油刀

        // 空出状态栏高度
        if (findViewById(R.id.StatusBarTextView) != null){

            int resourceId = this.getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                ((TextView)findViewById(R.id.StatusBarTextView)).setHeight(this.getResources().getDimensionPixelSize(resourceId));
            }
        }


    }

    /**
     * 设置布局
     */
    public abstract int intiLayout();

    /**
     * ButterKnife解绑
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();

        SwipeLayoutManager.getInstance().closeCurrentLayout();
        SwipeLayoutManager.getInstance().clearCurrentLayout();
        mCompositeSubscriptions.unsubscribe();
        unbinder.unbind();
    }

    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("android:support:fragments", null);
    }

    @Override
    public void setRequestedOrientation(int requestedOrientation) {
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return;
        }
        super.setRequestedOrientation(requestedOrientation);
    }

    /**
     * 解决 谷歌在安卓8.0版本时为了支持全面屏，增加了一个限制：如果是透明的Activity，则不能固定它的方向，
     * 因为它的方向其实是依赖其父Activity的（因为透明）。然而这个bug只有在8.0中有，8.1中已经修复。
     *
     * @return
     */
    private boolean isTranslucentOrFloating() {
        boolean isTranslucentOrFloating = false;
        try {
            int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
            final TypedArray ta = obtainStyledAttributes(styleableRes);
            Method m = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
            m.setAccessible(true);
            isTranslucentOrFloating = (boolean) m.invoke(null, ta);
            m.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isTranslucentOrFloating;
    }


}
